package com.njust.RateMyTray_backend.service.impl;

import com.njust.RateMyTray_backend.common.ResourceNotFoundException;
import com.njust.RateMyTray_backend.service.CommentService;
import com.njust.RateMyTray_backend.service.DishService;
import com.njust.RateMyTray_backend.service.ValidationService;
import com.njust.RateMyTray_backend.service.WindowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
public class ValidationServiceImpl implements ValidationService {

    @Autowired
    @Lazy // 使用 @Lazy 来避免可能的循环依赖
    private DishService dishService;

    @Autowired
    @Lazy
    private WindowService windowService;
    
    @Autowired
    @Lazy
    private CommentService commentService;

    @Override
    public void validateTargetExists(String targetType, Long targetId) {
        String resourceName;
        boolean exists;

        switch (targetType.toLowerCase()) {
            case "dish":
                resourceName = "菜品";
                exists = dishService.getById(targetId) != null;
                break;
            case "window":
                resourceName = "窗口";
                exists = windowService.getById(targetId) != null;
                break;
            case "comment":
                resourceName = "评论";
                exists = commentService.getById(targetId) != null;
                break;
            default:
                throw new IllegalArgumentException("无效的对象类型: " + targetType);
        }

        if (!exists) {
            throw new ResourceNotFoundException(resourceName + "不存在");
        }
    }
} 